package com.card.admin;

import org.jboss.netty.channel.ChannelHandlerContext;

import com.xiaoao.net.config.ApplicationConfiguration;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.StringUtil;
import com.xiaoao.net.utils.URLUtil;
import com.xiaoao.net.utils.json.GSONUtils;

public class ServerManager {
	public static final boolean CheckV = ApplicationConfiguration.Properties().GetBoolean(
			ServerManager.class, "CheckV");
	public static final String AppId = ApplicationConfiguration.Properties().GetString(
			ServerManager.class, "AppId");
	public static final String Token = ApplicationConfiguration.Properties().GetString(
			ServerManager.class, "Token");
	
	private static ServerManager instance = new ServerManager();
    public static ServerManager getInstance() {
        return instance;
    }
    private UpgradeInfo upgradeInfo;
    
    public boolean checkLogin(int loginType,int versionCode,ChannelHandlerContext ctx) {
    	if(1>0) {
    		return true;
    	}
    	if(loginType == 2) {
    		return IosServerManager.getInstance().checkLogin(versionCode, ctx);
    	}
    	UpgradeInfo info = upgradeInfo;
    	if(info != null) {
    		int apkVersion = PubFunc.parseInt(info.getVersion());
    		if(info != null
    				&& versionCode < apkVersion
    				) {
    			return info.sendUpgradeMessage(versionCode,ctx);
    		}
    	}
    	return true;
    }
    
    public UpgradeInfo load() {
    	try {
    		if(AppId == null) {
    			return null;
    		}
    		String url = "http://api.fir.im/apps/latest/"+AppId+"?api_token="+Token;
    		String json = URLUtil.urlGet(url);
    		this.upgradeInfo = GSONUtils.FromJSONString(json, UpgradeInfo.class);
    		return upgradeInfo;
		} catch (Exception e) {
		}
    	return null;
    }

	public static void main(String[] args) {
		long now = System.currentTimeMillis();
		UpgradeInfo info = ServerManager.getInstance().load();
		System.err.println("进行："+(System.currentTimeMillis()-now));
		System.err.println(PubFunc.parseInt(info.getVersion())>2);
		System.err.println(info.toString());
	}
}

